package com.sixth.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sixth.core.entity.Medicines;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.service.MedicinesService;
import com.sixth.stock.vo.DespensingVo;
import com.sixth.stock.vo.MedicinesForInventoryVo;
import com.sixth.stock.vo.MedicinesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Api(tags = "药品信息管理")
@RestController
@RequestMapping("/stock/medicinal")
public class MedicinesController {
    @Resource
    private MedicinesService medicinesService;

    //药品信息的条件查询
    @ApiOperation(value = "药品信息的条件查询,分页" )
    @PostMapping("/list")
    public R<IPage<Medicines>> selectByPage(@RequestBody MedicinesVo medicinesVo) {
        return medicinesService.selectByPage(medicinesVo);
    }
    @ApiOperation(value = "药品信息的条件查询,分页,库存页面用，有时间的查询" )
    @PostMapping("/list2")
    public R<IPage<Medicines>> selectByPage(@RequestBody MedicinesForInventoryVo medicinesVo) {
        return medicinesService.selectByPage2(medicinesVo);
    }
@ApiOperation(value = "药品信息的条件查询,分页为5" )
    //初始药品信息查询，分页为5
    @GetMapping("/init")
    public R<IPage<Medicines>> init() {
        MedicinesVo medicinesVo = new MedicinesVo();
        return medicinesService.selectInit();
    }

    //所有的药品信息,不加任何条件
    @ApiOperation(value = "所有的药品信息,不加任何条件" )
    @GetMapping("/all")
    public R<List<Medicines>> selectAll() {
        return R.ok(medicinesService.list());
    }
    //所有可用药品信息，指未删除，且可用即状态为0
    @ApiOperation(value = "所有可用药品信息，指未删除，且可用即状态为0" )
    @GetMapping("/allAvailable")
    public List<Medicines>  selectAllAvailable() {
        return medicinesService.listAvailable();
    }
    //根据id查询药品信息
    @ApiOperation(value = "根据id查询药品信息" )
    @PostMapping("/getMedicinesById/{id}")
    public R<Medicines> get(@PathVariable Long id) {
        return R.ok(medicinesService.getById(id));
    }
    ///批量删除,传入药品id的数组
    @ApiOperation(value = "批量删除,传入药品id的数组" )
    @PostMapping("/batchDelete")
    public R<String> batchDelete(@RequestBody Long[] value) {
        return medicinesService.batchDelete(value);
    }

    //单个删除,根据id删除
    @ApiOperation(value = "单个删除,根据id删除" )
    @DeleteMapping("/delete/{id}")
    public R<String> delete(@PathVariable Long id) {
        return medicinesService.deleteById(id);
    }
    //修改药品信息
    @ApiOperation(value = "修改药品信息" )
    @PutMapping("/update")
    public R update(@RequestBody Medicines medicines) {
        boolean flag = medicinesService.updateById(medicines);
        return flag ? R.ok("修改成功") : R.error("修改失败,请重试,或者联系管理员");
        //调用自带的方法
    }

    //新增药品信息,新增时库存为0
    @ApiOperation(value = "新增药品信息,新增时库存为0" )
    @PostMapping("/add")
    public R add(@RequestBody Medicines medicines) {
        String username = WebUtil.getUsername();
        medicines.setMedicinesStockNum(0);
        if (medicines.getCreateTime() == null)  {
            medicines.setCreateTime(new Date());
            medicines.setCreateBy(username);
        } else {
            medicines.setUpdateTime(new Date());
            medicines.setUpdateBy(username);
        }
        return R.ok(medicinesService.save(medicines));
    }
   //调整·库存,传入药品id和调整后的库存值
    @ApiOperation(value = "调整库存,传入药品id和调整后的库存值" )
    @PutMapping("/updateStock/{id}/{stock}")
    public R updateStock(@PathVariable( "id") Long id,@PathVariable("stock") Integer stock){
        return medicinesService.updateStock(id, stock);
    }
    //药品出库,传入药品id和出库数量,减少库存
    @ApiOperation(value = "药品出库,传入药品id和出库数量,减少库存" )
    @PostMapping("/dispensing")
    public R despensing(@RequestBody List<DespensingVo> despensingVos){
        System.out.println("+++++++++++++++++++");
        System.out.println(despensingVos.toString());
        return medicinesService.despensing(despensingVos);
    }
}
